//
//  VisitorView.swift
//  weibo
//
//  Created by mac on 16/8/3.
//  Copyright © 2016年 zl. All rights reserved.
//

import UIKit

class VisitorView: UIView {
    
    
    
    //设置每个访客界面的信息
    func setupViewInfo(imgName : String? ,title : String) {
        
        titleLabel.text = title
        
        guard let imageName = imgName else{
            
            startAnim()
            
            return
        }
        
        
        imgView.image = UIImage(named: imageName)
        
        homeImgView.hidden = true
        
        sendSubviewToBack(coverImgView)
        
        
    }
    
    //添加动画
    func startAnim () {

        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        
        
        anim.toValue = 2 * M_PI
        
        anim.repeatCount = MAXFLOAT
        
        anim.duration = 20
        
        anim.removedOnCompletion = false
        
        imgView.layer.addAnimation(anim, forKey: nil)
    }
    

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUIView()
    }

    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
    }
    
    
    var controller : UIViewController?
    
    //懒加载子控件
    
    private lazy var imgView : UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    
    //设置遮罩图片
    private lazy var coverImgView: UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
    private lazy var homeImgView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    
//    private var titleLabel : UILabel = {
//       let label = UILabel()
//        
//        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
//        label.textColor = UIColor .darkGrayColor()
//        label.font = UIFont .systemFontOfSize(14)
//
//        
//        
//        return  label
//    }()
    
    private lazy var titleLabel :UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")
    
    
//    private lazy var registBtn : UIButton = {
//        
//       let button = UIButton()
//        button.setTitle("注册", forState: UIControlState.Normal)
//        
//        button.setTitleColor(UIColor .orangeColor(), forState: UIControlState.Normal)
//       
//        
//        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
//            
//        return button
//    }()
    
     lazy var registBtn :UIButton = UIButton(title: "注册", color: UIColor .orangeColor(), imgName: "common_button_white_disable")
    
//    private lazy var loginBtn : UIButton = {
//       
//        let button = UIButton()
//        
//        button.setTitle("登录", forState: UIControlState.Normal)
//        
//        button.setTitleColor(UIColor .darkGrayColor(), forState: UIControlState.Normal)
//        
//        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
//        
//        
//        
//        return button
//        
//    }()
    
         lazy var loginBtn :UIButton = UIButton(title: "登录", color: UIColor .darkGrayColor(), imgName: "common_button_white_disable")

}




extension VisitorView {
    
    private func setupUIView () {
        
        //1.添加子控件
        addSubview(imgView)
        addSubview(coverImgView)
        addSubview(homeImgView)
        addSubview(titleLabel)
        addSubview(registBtn)
        addSubview(loginBtn)
        
        
//        registBtn.addTarget(self, action: "visitorDidRegist", forControlEvents: .TouchUpInside)
//        loginBtn.addTarget(self, action: "visitorDidLogin", forControlEvents: .TouchUpInside)
        
        //2.设置约束
        for v in subviews {
           v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        
        //外部图片
        addConstraint(NSLayoutConstraint(item: imgView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: imgView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        
        //内部图片
        addConstraint(NSLayoutConstraint(item: homeImgView, attribute: .CenterX, relatedBy: .Equal, toItem: imgView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeImgView, attribute: .CenterY, relatedBy: .Equal, toItem: imgView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        //titlelabel
        
 

        
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .CenterX, relatedBy: .Equal, toItem: imgView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Top, relatedBy: .Equal, toItem: imgView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        
        //注册按钮
        
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: .Left, relatedBy: .Equal, toItem: titleLabel, attribute: .Left, multiplier: 1.0, constant: 0))

        addConstraint(NSLayoutConstraint(item: registBtn, attribute: .Top, relatedBy: .Equal, toItem: titleLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        
        //登录按钮
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Right, relatedBy: .Equal, toItem: titleLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: titleLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        
        
        //添加遮罩按钮
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask":coverImgView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(btnHight)-[registBtn]", options: [], metrics: ["btnHight":-36], views: ["mask":coverImgView,"registBtn":registBtn]))
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        
    }
}


